import { t } from 'i18next'
import React from 'react'
import {
	IconActionDcs,
	IconActionDcsApply,
	IconActionDcsRefuse,
	IconActionDelete,
	IconActionOffline,
	IconActionOnline,
} from '../../../../../../components'
import { DcsModel } from '@/api/manager/dcs/interface'
import { Mt } from '@/api/manager/mt'
import { useMccStore } from '@/pages/manager/store'
import useLock from '@/hooks/useLock'
import { Confirm } from '@/components/Alert'
import {
	fetchCallDcsMts,
	fetchCancelDcsMts,
	fetchHangupDcsMts,
	fetchApplyDcsMts,
	fetchApplyDcs,
	fetchRefuseDcs,
	fetchDeleteDcsMts,
} from '@/api/manager/dcs'
import { useTableDcsStore } from '../../store'

export type operateType = 'dcsApply' | 'dcsRefuse' | 'dcs' | 'delete' | 'online' | 'offline'

type ActionComponentType = Record<operateType, (mcc: Mt, options: { dcs: DcsModel['dcs'] }) => React.ReactNode>

export const OperateIconComponent: ActionComponentType = {
	dcsApply: (mcc, { dcs }) => {
		return <DcsApply mcc={mcc} dcs={dcs} key="dcsApply" />
	},
	dcsRefuse: (mcc, { dcs }) => {
		return <DcsRefuse mcc={mcc} dcs={dcs} key="dcsRefuse" />
	},
	dcs: (mcc, { dcs }) => {
		return <Dcs mcc={mcc} dcs={dcs} key="dcs" />
	},
	delete: (mcc, { dcs }) => {
		return <Delete mcc={mcc} dcs={dcs} key="delete" />
	},
	online: (mcc, { dcs }) => {
		return <Online mcc={mcc} dcs={dcs} key="online" />
	},
	offline: (mcc, { dcs }) => {
		return <Offline mcc={mcc} dcs={dcs} key="offline" />
	},
}
const DcsApply = ({ mcc, dcs }: { mcc: Mt; dcs: DcsModel['dcs'] }) => {
	const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	const onClick = useLock(async () => {
		await fetchApplyDcs(meetingMoid, mcc)
	})
	return <IconActionDcsApply title={t('同意参与协作')} onClick={onClick} />
}
const DcsRefuse = ({ mcc, dcs }: { mcc: Mt; dcs: DcsModel['dcs'] }) => {
	const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	const onClick = useLock(async () => {
		await fetchRefuseDcs(meetingMoid, mcc)
	})
	return <IconActionDcsRefuse title={t('拒绝参与协作')} onClick={onClick} />
}
const Dcs = ({ mcc, dcs }: { mcc: Mt; dcs: DcsModel['dcs'] }) => {
	const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	const onClick = useLock(async () => {
		dcs.isDcs ? await fetchCancelDcsMts(meetingMoid, [mcc]) : await fetchApplyDcsMts(meetingMoid, [mcc])
	})
	return <IconActionDcs isOn={dcs.isDcs} title={dcs.isDcs ? t('退出协作') : t('参与协作')} onClick={onClick} />
}
const Delete = ({ mcc, dcs }: { mcc: Mt; dcs: DcsModel['dcs'] }) => {
	const onClick = useLock(async () => {
		const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
		Confirm(t('确定要删除 {{name}}吗？', { name: mcc.name }), () => {
			fetchDeleteDcsMts(meetingMoid, [mcc])
		})
	})
	return <IconActionDelete title={t('删除终端')} onClick={onClick} />
}
const Online = ({ mcc, dcs }: { mcc: Mt; dcs: DcsModel['dcs'] }) => {
	const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	const onClick = useLock(async () => {
		let text = mcc.accountType === 6 ? t('确定要断开 ') : t('确定要挂断 ')
		Confirm(text, () => {
			fetchHangupDcsMts(meetingMoid, [mcc])
		})
	})
	return <IconActionOnline title={t('挂断')} onClick={onClick} />
}
const Offline = ({ mcc, dcs }: { mcc: Mt; dcs: DcsModel['dcs'] }) => {
	const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
	const onClick = useLock(async () => {
		useTableDcsStore.getState().tableItemCalling(mcc.mtKey)
		await fetchCallDcsMts(meetingMoid, [mcc])
	})
	return <IconActionOffline title={t('呼叫')} onClick={onClick} />
}
